package com.arina.onepagesmarthome;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class RoomList extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.roomlist);
		
		readXML("systemstructure.xml");
//		ImageView bg=(ImageView)findViewById(R.id.bg_image);
//		bg.setOnTouchListener(new OnSwipeTouchListener() {
//			
//			@Override
//			public void onSwipeLeft() {
//				Toast.makeText(getApplicationContext(), "left", Toast.LENGTH_SHORT).show();
//				Intent i = new Intent(RoomList.this,OnePageSmarthome.class);
////				Bundle bundle=new Bundle();
////
////				bundle.putParcelable("system", system);
////				bundle.putInt("network", mNETWORK_STATUS.ordinal());
////				bundle.putInt("timepost", timepost);
////				i.putExtras(bundle);
//
//				startActivity(i);
//				finish();
//				
//			}
//			@Override
//			public void onSwipeBottom() {
//				Toast.makeText(getApplicationContext(), "bottom", Toast.LENGTH_SHORT).show();
//			}
//		});
		
		Button btPhongKhach=(Button)findViewById(R.id.btphongkhach);
		btPhongKhach.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iPhongKhach=new Intent(RoomList.this,PhongKhach_activity.class);
				startActivity(iPhongKhach);
				//finish();
			}
		});
		
		Button btSanVuon=(Button)findViewById(R.id.btsanvuon);
		btSanVuon.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iSanVuon=new Intent(RoomList.this,SanVuon_activity.class);
				startActivity(iSanVuon);
				//finish();
			}
		});
		
		Button btPhongNguLau1=(Button)findViewById(R.id.btphongngulau1);
		btPhongNguLau1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iPhongNguLau1=new Intent(RoomList.this,phongngulau1_activity.class);
				startActivity(iPhongNguLau1);
				//finish();
			}
		});
		
		Button btPhongTamLau1=(Button)findViewById(R.id.btphongtamlau1);
		btPhongTamLau1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iPhongTamLau1=new Intent(RoomList.this,phongtamlau1_activity.class);
				startActivity(iPhongTamLau1);
				//finish();
			}
		});
		
		Button btPhongNguLau2=(Button)findViewById(R.id.btphongngulau2);
		btPhongNguLau2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iPhongNguLau2=new Intent(RoomList.this,phongngulau2_activity.class);
				startActivity(iPhongNguLau2);
				//finish();
			}
		});
		
		Button btPhongTamLau2=(Button)findViewById(R.id.btphongtamlau2);
		btPhongTamLau2.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent iPhongTamLau2=new Intent(RoomList.this,phongtamlau2_activity.class);
				startActivity(iPhongTamLau2);
				//finish();
			}
		});
		
		if (savedInstanceState != null) {
			return;
		}
	}

	public void readXML(String assetXMLFileName)
	{
		XmlPullParserFactory pullParserFactory;
		try {
			pullParserFactory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = pullParserFactory.newPullParser();

			InputStream in_s = getAssets().open(assetXMLFileName);
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			parser.setInput(in_s, null);

			parseXML(parser);

		} catch (XmlPullParserException e) {

			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private  void parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
	{
		//globaldata data = new globaldata();
		int eventType = parser.getEventType();
		int index=0;
		int indexSSID=0;
		
		SYSTEM_TYPE systemType = null;
		String localIP = null;
		int localPort = 4321;
		int broadcastPort = 6000;
		String externalIP = null;
		
		while (eventType != XmlPullParser.END_DOCUMENT)
		{
			String name = null;

			if( eventType==XmlPullParser.START_TAG)
			{
				name = parser.getName();
				if (name.equalsIgnoreCase("name"))
				{
					((globaldata) this.getApplication()).homename=parser.nextText();
				} 
				if (name.equalsIgnoreCase("rooms")){
					int numRooms=Integer.parseInt(parser.getAttributeValue(null, "numberLayout"));
					((globaldata) this.getApplication()).rooms=new room[numRooms];
				}
				if(name.equalsIgnoreCase("room"))
				{
					String layoutName=parser.getAttributeValue(null, "layout");
					String layoutTitle=parser.nextText();
					room temproom=new room(layoutName, layoutTitle);
					((globaldata) this.getApplication()).rooms[index]=temproom;
					index++;
				}
				if(name.equalsIgnoreCase("houseowner"))
				{
					((globaldata) this.getApplication()).homeowner=parser.getAttributeValue(null, "name");
				}
				if(name.equalsIgnoreCase("ssids"))
				{
					int numSSID=Integer.parseInt(parser.getAttributeValue(null, "numberSSID"));
					((globaldata) this.getApplication()).ssid=new String[numSSID];
				}
				if(name.equalsIgnoreCase("ssid"))
				{
					((globaldata) this.getApplication()).ssid[indexSSID]=parser.getAttributeValue(null, "name");
				}
				if(name.equalsIgnoreCase("system"))
				{
					String systemtype=parser.getAttributeValue(null, "type");
					if(systemtype.equalsIgnoreCase("sbus"))
						systemType=SYSTEM_TYPE.SBUS;
					else
						systemType=SYSTEM_TYPE.ARINA;
					localIP=parser.getAttributeValue(null, "ip");
					externalIP=parser.getAttributeValue(null, "ddns");
					localPort=Integer.parseInt(parser.getAttributeValue(null, "controlport"));
					broadcastPort=Integer.parseInt(parser.getAttributeValue(null, "broadcastport"));
					((globaldata) this.getApplication()).masterboard.add(new MasterBoard(systemType,localIP, externalIP, localPort, broadcastPort));
				}
			}
			
			eventType = parser.next();
		}
		
	}
}
